package com.jcfk.eam.util;

import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;

@NoArgsConstructor
public class BigDecimalUtils {
    /**
     * 数字和
     *
     * @param start
     * @param end
     * @return
     */
    public static BigDecimal add(BigDecimal start, BigDecimal end) {
        if (start == null) {
            start = BigDecimal.ZERO;
        }
        if (end == null) {
            end = BigDecimal.ZERO;
        }
        return start.add(end);
    }

    /**
     * 数字除
     *
     * @param start
     * @param end
     * @param scale
     * @param roundingMode
     * @param defaultValue
     * @return
     */
    public static BigDecimal divide(BigDecimal start, BigDecimal end, int scale, RoundingMode roundingMode, BigDecimal defaultValue) {
        if (start == null || start.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        if (end == null || end.compareTo(BigDecimal.ZERO) == 0) {
            return defaultValue;
        }
        return start.divide(end, scale, roundingMode);
    }

    /**
     * 比例
     *
     * @param start
     * @param end
     * @return
     */
    public static BigDecimal ratio(BigDecimal start, BigDecimal end) {
        if (start == null || start.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        if (end == null || end.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        return start.multiply(BigDecimal.valueOf(100)).divide(end, 2, RoundingMode.HALF_UP);

    }
}
